package cn.it.hbtt.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.it.hbtt.dao.BaseDAO;
import cn.it.hbtt.entity.User;

@WebServlet("/Regist")
public class RegistServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String loginName = request.getParameter("loginName");
		String pwd = request.getParameter("password");
		String rwd = request.getParameter("repsword");
		String email = request.getParameter("email");
		String mobile = request.getParameter("mobile");
		//String userName = request.getParameter("userName");
		//String identityCode = request.getParameter("identityCode");
        //int identityCode2 = Integer.parseInt(identityCode);
        
		try {

			BaseDAO base = new BaseDAO();
			Connection conn = base.getConnection();
			System.out.println(conn);

			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery("select * from easybuy_user");
			List<User> list = new ArrayList<User>();

			while (rs.next() == true) {
				User g = new User();
				g.setId(rs.getInt("id"));
				g.setLoginName(rs.getString("loginName"));
				g.setUserName(rs.getString("userName"));
				g.setPassword(rs.getString("password"));
				g.setSex(rs.getInt("sex"));
				g.setIdentityCode(rs.getString("identityCode"));
				g.setEmail(rs.getString("email"));
				g.setMobile(rs.getString("mobile"));
				g.setType(rs.getInt("type"));
				list.add(g);
			}

			String loginName1 = null;
			int id1 = 0;
			for (int i = 0; i < list.size(); i++) {
				User u = (User) list.get(i);
				loginName1 = u.getLoginName();
				  if (loginName.equals(loginName1)) {
				  break; 
				  }
			}
				if (loginName.equals(loginName1)) {
					String str = "<script  type='text/javascript'>";
					str += "alert('该用户已被注册!');";
					str += "location.href='http://localhost:8080/easybuy/Regist.jsp'";
					str += "</script>";
					out.write(str);
					out.flush();
				} 

			if (rwd.equals(pwd)) {
				try {
					System.out.println(conn);
					String sql = "insert into easybuy_user (loginName,password,email,mobile) values(?,?,?,?)";
					PreparedStatement ps = conn.prepareStatement(sql);
					ps.setString(1, loginName);
					// 为sql语句中的问号赋值
					ps.setString(2, pwd);
					ps.setString(3, email);
					ps.setString(4, mobile);
					// 执行sql语句
		            ps.executeUpdate();
		            // 关闭数据库连接对象
					conn.close();
					String str = "<script  type='text/javascript'>";
					str += "alert('注册成功,请牢记账户信息！');";
					str += "location.href='http://localhost:8080/easybuy/Login.jsp'";
					str += "</script>";
					out.write(str);
					out.flush();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}else {
				String str = "<script  type='text/javascript'>";
				str += "alert('两次密码不同，请重新输入!');";
				str += "location.href='http://localhost:8080/easybuy/Regist.jsp'";
				str += "</script>";
				out.write(str);
				out.flush();
			}
			 
			 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		doGet(request, response);
	}

}
